//
//  TestDb.m
//  JhonSell
//
//  Created by Mario Montoya on 7/01/09.
//  Copyright 2009 El malabarista. All rights reserved.
//

#import "Db.h"
#import "TestBaseORM.h"

@interface TestDb : TestBaseORM {}
@end

@implementation TestDb

-(void) testExecute{
	Db *db = [Db currentDb];
	
	//Check is assigned the name to the global class
	GHAssertNotNil([Db currentDb],@"Must be set");
	
	[db execute:@"CREATE TABLE IF NOT EXISTS test ('id' INTEGER)"];
	[db execute:@"DELETE FROM test"];
	[db execute:@"INSERT OR REPLACE INTO test ('id') VALUES (1)"];	
}

-(void) testCount{
	Db *db = [Db currentDb];

	[self testExecute];
	GHAssertEquals([db count:@"test"],1,@"Must be 1");
	[db execute:@"INSERT INTO test ('id') VALUES (1)"];
	GHAssertEquals([db count:@"test"],2,@"Must be 2");
}

-(void) testQuery{
	Db *db = [Db currentDb];

	FMResultSet *rs = [db load:@"SELECT 1"];
	@try {
		GHAssertEquals([rs next],YES,@"Row not found");
		GHAssertEquals([rs intForColumn:@"1"],1,@"Incorrect value");
	} 
	@finally {
		[rs close];
	}
}

-(void) testReadUserVersion {
	Db *db = [Db currentDb];
	[db execute:@"PRAGMA user_version=55"];
	
	GHAssertEquals([db userDatabaseVersion],55,@"Fail to read db version");
}

@end
